﻿
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace QntSvc.Web
{
    public class OnlineHub:Hub
    {
        public Task RobotLogSubs(long robotId)
        {
        //Console.WriteLine(    GlobalHost.ConnectionManager)
        //  Microsoft.AspNetCore.SignalR.DefaultHubLifetimeManager.
            return Groups.AddToGroupAsync(Context.ConnectionId, GetRobotLogGropuName(robotId));
        }
        public override Task OnConnectedAsync()
        {
            return base.OnConnectedAsync();
        }
        public override Task OnDisconnectedAsync(Exception exception)
        {
            return base.OnDisconnectedAsync(exception);
        }
        public OnlineHub()
        {
            //if (OnlineHubSender==null)
            //{
            //    OnlineHubSender = this;
            //}
        }
        public static string GetRobotLogGropuName(long robotId) {
            return "Log_RobotId:" + robotId;
        }
        public static IHubContext<OnlineHub> OnlineHubSender { get; set; }
        public static void SendRobotLogToGuoup(QntPlatform.Db.LogInfo e)
        {
            if (OnlineHub.OnlineHubSender != null)
            {
                OnlineHub.OnlineHubSender.Clients.Group(OnlineHub.GetRobotLogGropuName(e.OwnerId)).SendAsync("robotLog", e);
            }
        }
        //机器人id,log
      //  public static event EventHandler<KeyValuePair<long, string>> LogWriteEvent;
    }

    public class CallingSideClass
    {
        private readonly IHubContext<OnlineHub> _hubContext;

        public CallingSideClass(IHubContext<OnlineHub> hubContext)
        {
            _hubContext = hubContext; 
        }

        public async Task FooMethod()
        {
            await _hubContext.Clients.All.SendAsync("");
        }
    }
}
